<template>
  <div class="favorites-page">
    <!-- 导航栏 -->
    <div class="navbar">
      <div class="navbar-content">
        <div class="navbar-left">
          <button @click="$router.back()" class="back-button">
            <i class="fas fa-arrow-left"></i>
          </button>
          <h1 class="page-title">我的收藏</h1>
        </div>
        <button class="clear-button" @click="clearFavorites">清空</button>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-area">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <p class="loading-text">正在加载数据...</p>
      </div>

      <!-- 收藏列表 -->
      <div v-else-if="favorites.length > 0" class="favorites-list">
        <div class="favorite-item" v-for="item in favorites" :key="item.id" @click="toPlayer(item.id)">
          <div class="item-content">
            <div class="cover-container">
              <img :src="item.cover" class="cover-image" :alt="item.title" />
              <div class="star-badge">
                <i class="fas fa-star"></i>
              </div>
            </div>
            <div class="item-info">
              <h4 class="item-title">{{ item.title }}</h4>
              <p class="item-meta">
                <span class="tag" v-for="(value, index) in item.tags" :key="index">{{ value }}</span>
                · 全{{ item.totalEpisodes }}集
              </p>
              <div class="item-footer">
                <span class="favorite-time">收藏于 {{ item.favoriteTime }}</span>
                <div class="action-buttons">
                  <button class="watch-button">观看</button>
                  <button class="unfavorite-button" @click.stop="removeFavorite(item)">取消收藏</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <i class="fas fa-star empty-icon"></i>
        <h3 class="empty-title">暂无收藏内容</h3>
        <p class="empty-desc">发现喜欢的内容，点击收藏吧</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reqUserFavorites, reqDeleteFavorite, reqBatchDeleteFavorites } from '@/apis/video'

const router = useRouter()

// 收藏列表
const favorites = ref([])
// 加载状态
const loading = ref(true)

const toPlayer = (id) => {
  router.push({ name: 'Player', params: { dramaId: id } })
}

// 清空收藏
const clearFavorites = () => {
  showConfirmDialog({
    title: '标题',
    message: '确定要清空收藏记录吗？'
  })
    .then(async () => {
      // const response = await reqDeleteFavorite(item.favoriteType, item.id || item.dramaId)
      // console.log('取消收藏响应:', response)
      // favorites.value.splice(index, 1)
      // showToast(`已取消收藏《${item.title}》`)

      const ids = favorites.value.map((item) => ({ type: item.favoriteType, id: item.id || item.dramaId }))
      reqBatchDeleteFavorites(ids)

      favorites.value = []
      showToast('已清空收藏列表')
    })
    .catch(() => {
      // on cancel
    })
}

// 取消收藏
const removeFavorite = (item) => {
  const index = favorites.value.findIndex((f) => f.id === item.id)
  if (index !== -1) {
    showConfirmDialog({
      title: '标题',
      message: '确定要删除该收藏记录吗？'
    })
      .then(async () => {
        const response = await reqDeleteFavorite(item.favoriteType, item.id || item.dramaId)
        console.log('取消收藏响应:', response)
        favorites.value.splice(index, 1)
        showToast(`已取消收藏《${item.title}》`)
      })
      .catch(() => {
        // on cancel
      })
  }
}

// 获取收藏列表
const fetchFavorites = async () => {
  try {
    loading.value = true
    const response = await reqUserFavorites()
    console.log('收藏列表:', response.data.list)
    favorites.value = response.data.list || []

    if (!response.data.list || response.data.list.length === 0) {
      showToast('暂无收藏内容')
    }
  } catch (error) {
    console.error('获取收藏列表失败:', error)
    showToast('获取数据失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchFavorites()
})
</script>

<style scoped lang="scss">
.favorites-page {
  width: 100%;
  min-height: 100vh;
  background-color: #f9fafb;
  position: relative;

  .navbar {
    background-color: white;
    border-bottom: 1px solid #f3f4f6;

    &-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 12px 16px;
    }

    &-left {
      display: flex;
      align-items: center;
    }
  }

  .back-button {
    margin-right: 16px;
    color: #6b7280;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 16px;
  }

  .page-title {
    font-size: 18px;
    font-weight: 600;
    color: #1f2937;
    margin: 0;
  }

  .clear-button {
    color: #2563eb;
    font-size: 14px;
    font-weight: 500;
    background: none;
    border: none;
    cursor: pointer;
  }

  .content-area {
    padding: 24px 16px 80px;
  }

  .favorites-list {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .favorite-item {
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    cursor: pointer;

    .item-content {
      display: flex;
      padding: 16px;
    }

    .cover-container {
      width: 80px;
      height: 96px;
      background: linear-gradient(135deg, #10b981, #0d9488);
      border-radius: 8px;
      margin-right: 12px;
      flex-shrink: 0;
      position: relative;
      overflow: hidden;

      .cover-image {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .star-badge {
        position: absolute;
        top: 4px;
        right: 4px;
        background-color: #eab308;
        color: white;
        font-size: 12px;
        padding: 2px 4px;
        border-radius: 4px;
      }
    }

    .item-info {
      flex: 1;
      min-width: 0;

      .item-title {
        font-weight: 500;
        color: #1f2937;
        margin: 0 0 4px 0;
        font-size: 16px;
      }

      .item-meta {
        font-size: 14px;
        color: #6b7280;
        margin: 0 0 8px 0;

        .tag {
          margin-right: 4px;
        }
      }

      .item-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .favorite-time {
          font-size: 14px;
          color: #9ca3af;
        }

        .action-buttons {
          display: flex;
          gap: 8px;

          .watch-button {
            color: #2563eb;
            font-size: 14px;
            background: none;
            border: none;
            cursor: pointer;
            transition: color 0.2s;

            &:hover {
              color: #1d4ed8;
            }
          }

          .unfavorite-button {
            color: #dc2626;
            font-size: 14px;
            background: none;
            border: none;
            cursor: pointer;
            transition: color 0.2s;

            &:hover {
              color: #b91c1c;
            }
          }
        }
      }
    }
  }

  .loading-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 3rem 0;
    height: 30vh;

    .loading-spinner {
      width: 40px;
      height: 40px;
      border: 3px solid rgba(0, 0, 0, 0.1);
      border-top-color: #f97316;
      border-radius: 50%;
      animation: spin 1s ease-in-out infinite;
      margin-bottom: 1rem;
    }

    .loading-text {
      color: #666;
      font-size: 1rem;
    }
  }

  @keyframes spin {
    to {
      transform: rotate(360deg);
    }
  }

  .empty-state {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 3rem 0;
    height: 30vh;
    text-align: center;

    .empty-icon {
      color: #d1d5db;
      font-size: 3rem;
      margin-bottom: 1rem;
    }

    .empty-title {
      color: #6b7280;
      font-weight: 500;
      margin: 0 0 8px 0;
      font-size: 16px;
    }

    .empty-desc {
      color: #9ca3af;
      font-size: 14px;
      margin: 0;
    }
  }

  .toast-message {
    position: fixed;
    bottom: 5rem;
    left: 50%;
    transform: translateX(-50%);
    background-color: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    z-index: 50;
  }
}
</style>
